package controller;

import controller.actions.Action;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class Start extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected ActionFactory factory = new ActionFactory();

    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        Action action = factory.create(getActionName(request));
        String url = action.perform(request, response);
        if (url != null)
            getServletContext().getRequestDispatcher(url).forward(request, response);
    }

    protected String getActionName(HttpServletRequest request) {
        String path = request.getServletPath();
        return path.substring(1, path.lastIndexOf("."));
    }
}
